//
//  VideoViewController.m
//  Four_Fit
//
//  Created by xalo on 16/7/12.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "VideoViewController.h"

#define kVideoURL @"http://bea.sportq.com/SFitWeb/sfit/getFind?form.device=&form.mySex=0&form.phoneType=1&form.sex=0&form.sign=BDCEE12B8B31662E7459743224327E1E&form.userId=93118&form.version=1.1.5"
#define kVWidth [UIScreen mainScreen].bounds.size.width
#define kVHeight [UIScreen mainScreen].bounds.size.height

@interface VideoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)NSMutableDictionary *recommendDic; //推荐视图信息数组
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataSource; //数据源
@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self loadData];
    //collectionView
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collectionView];
    //headerView
    [self.collectionView registerNib:[UINib nibWithNibName:@"VideoHeaderView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//获取数据
- (void)loadData{
    [[QZRequestManager sharedManager] fetchDataWithUrl:kVideoURL parameters:nil method:@"GET" needToCaches:YES fetchData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        self.recommendDic = [dic[@"entRet"][@"lstCarousel"] firstObject];
    } updateUI:^{
        [self.collectionView reloadData];
    }];
}
#pragma mark 属性懒加载
- (NSDictionary *)recommendDic{
    if (!_recommendDic) {
        _recommendDic = [[NSMutableDictionary alloc] init];
    }
    return _recommendDic;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        //设置Layout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kVWidth - 2) / 2.0, (kVWidth - 2) / 2.0);
        layout.minimumInteritemSpacing = 2.0;
        layout.minimumLineSpacing = 2.0;
        //设置头大小
        layout.headerReferenceSize = CGSizeMake(kVWidth, kVHeight / 3.0 + 20);
        //collectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kVWidth, kVHeight) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor orangeColor];
    }
    return _collectionView;
}
#pragma mark  collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 8;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        VideoHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        [headerView.carouselImage sd_setImageWithURL:[NSURL URLWithString:self.recommendDic[@"carouselImageUrl"]]];
        headerView.planTitle.text = self.recommendDic[@"planTitle"];
        headerView.carouselSummary.text = self.recommendDic[@"carouselSummary"];
        headerView.carouselTagName.text = self.recommendDic[@"carouselTagName"];
        headerView.timeAndOther.text = [NSString stringWithFormat:@"%@分钟  %@卡路里  初级",self.recommendDic[@"trainDuration"],self.recommendDic[@"calorie"]];
        view = headerView;
    }
    return view;
}

@end
